package com.xiaoxiong.text;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;

import com.xiaoxiong.bean.BaseLeftRightData;
import com.xiaoxiong.database.OperateFormPersonalInfo;

public class DealPersonInfo {

	private Context context;
	private String html;
	private Document content;
	private String[] val;
	private List<BaseLeftRightData> personInfoMapList = new ArrayList<BaseLeftRightData>();
	private String[] titlename = new String[] { "学 号", "姓名", "年级", "专业", "身份证",
			"电子邮箱", "行政班", "班主任", "辅导员", "必修课的总学分", "必修课已修学分", "必修课已获得学分",
			"选修课已修学分", "选修课已获得学分", "累计已修学分", "累计已获得学分", "累计在读课程学分", "已选课程学分",
			"预期获得学分", "平均学分绩点", "毕业需修满学分" };

	public DealPersonInfo(Context context, String html) {
		super();
		this.context = context;
		this.html = html;
		this.content = Jsoup.parse(this.html);
		this.val = new String[21];
	}

	public void StartDeal() {
		Elements ele = content.getElementsByAttributeValue("align", "left");
		Elements element = content.getElementsByClass("td_left");

		int flag = 0;
		StringBuilder sb3 = new StringBuilder();

		for (Element links : ele) {
			if (flag < 3) {
				++flag;
				continue;
			}
			String title = links.text();
			sb3.append("[" + title + "]");
		}
//		Log.d("value", sb3.toString());

		for (int i = 0; i < 21; ++i) {

			if (i == 6) {
				val[i] = new String(element.get(8).text());
			} else {

				StringBuilder sbVal = new StringBuilder();
				int end = sb3.indexOf("]");
				for (int j = 1; j < end; ++j) {
					sbVal.append(sb3.charAt(j));
				}
				sb3.delete(0, end + 1);

				val[i] = new String(sbVal);
			}
			personInfoMapList.add(new BaseLeftRightData(titlename[i], val[i]));
		}

		//保存到数据库
		OperateFormPersonalInfo ofpi = new OperateFormPersonalInfo(context);
		ofpi.deleteAllPersonalInfo();
		int size = personInfoMapList.size();
		for (int i = 0; i < size; ++i) {
			ofpi.insertPersonalInfo(personInfoMapList.get(i).getLeftData(),
					personInfoMapList.get(i).getRightData());
		}

	}

}